<?php

/**
 * This is the model class for table "prefix_talk".
 *
 * The followings are the available columns in table 'prefix_talk':
 * @property string $talk_id
 * @property string $user_id
 * @property string $talk_title
 * @property string $talk_text
 * @property string $talk_date
 * @property string $talk_date_last
 * @property string $talk_user_ip
 * @property integer $talk_count_comment
 * @property User $user
 * @property array(User) $talkUsers
 */
class Talk extends LsActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Talk the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return Config::Get('db.table.talk');
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, talk_title, talk_text, talk_date, talk_date_last, talk_user_ip', 'required'),
			array('talk_count_comment', 'numerical', 'integerOnly'=>true),
			array('user_id', 'length', 'max'=>11),
			array('talk_title', 'length', 'max'=>200),
			array('talk_user_ip', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('talk_id, user_id, talk_title, talk_text, talk_date, talk_date_last, talk_user_ip, talk_count_comment', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(
				self::BELONGS_TO,
				'User',
				'user_id',
			),
			'users' => array(
				self::MANY_MANY,
				'User',
				Config::Get('db.table.talk_user').'(talk_id, user_id)',
			),
			'talkUsers' => array(
				self::HAS_MANY,
				'TalkUser',
				'talk_id',
			),
		/*	'talkUsers' => array(
				self::MANY_MANY,
				'User',
				Config::Get('db.table.talk_user').'(talk_id, user_id)',
			),*/
			'countFavourites' => array(
				self::STAT,
				'Favourite',
				'target_id',
				'condition' => '{%child}.target_type="talk"',
			),
			'favourites' => array(
				self::HAS_MANY,
				'Favourite',
				'target_id',
				'condition' => '{%child}.target_type="talk"',
			),
		);
	}
	
	
	public function getUrl(){
		return Router::GetPath('talk/read').$this->talk_id;
	}
	

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'talk_id' => 'Talk',
			'user_id' => 'User',
			'talk_title' => 'Talk Title',
			'talk_text' => 'Talk Text',
			'talk_date' => 'Talk Date',
			'talk_date_last' => 'Talk Date Last',
			'talk_user_ip' => 'Talk User Ip',
			'talk_count_comment' => 'Talk Count Comment',
		);
	}

}